import {getStorageSync, setStorageSync} from "../utils/Storage";
import Config from "../main.config";
import Ajax from "../utils/Ajax";
import {login as WxLogin, uploadFile, downloadFile} from "../utils/Wx";
let Common = {

    /*
    * wx.login 获取openId
    * */
    wxLogin: async function () {
        let url = Config.api.openId;
        let loginRes = await WxLogin();
        let res = await Ajax.request({
            method: "POST",
            url,
            data: {code: loginRes.code}
        }) || {}
        return res.openid
    },

    /*
    * 确认小程序登录完成 并获取到openId
    * */
    wxLoginComplete: function () {
        return new Promise(function (resolve) {
            const App = getApp();
            let openId = App.globalData.openId || getStorageSync(Config.storage.openId);
            if (openId) {
                Common.wxLoginComplete = async function () {return openId}
                resolve(openId);
            } else {
                App.getOpenIdCallback = function (res) {
                    Common.wxLoginComplete = async function () {return res};
                    resolve(res);
                }
            }
        })
    },

    /*
    * 微信快捷注册
    * */
    wxRegister: async function (data = {}) {
        let url = Config.api.wxRegister;
        return await Ajax.request({
            method: "POST",
            url,
            data: data
        })
    },

    /*
    * 根据openId 获取用户信息
    * */
    getUserInfo: async function (openId) {
        let url = Config.api.userInfo;
        let res = await Ajax.request({
            method: "POST",
            url,
            data: {openid: openId}
        })
        res ? setStorageSync(Config.storage.userInfo, res) : null;
        return res
    },

    /*
    * 发送短信验证码
    * */
    sendSms: async function (phone) {
        let url = Config.api.sms;
        return await Ajax.request({
            method: "POST",
            url,
            data: {phone: phone}
        })
    },

    /*
    * 注册
    * */
    register: async function (data = {}) {
        let url = Config.api.register;
        return await Ajax.request({
            method: "POST",
            url,
            data
        })
    },

    /*
    * 登录
    * */
    login: async function (data = {}) {
        const APP = getApp();
        let url = Config.api.login;
        let res = await Ajax.request({
            method: "POST",
            url,
            data
        })
        if (APP) {
            APP.globalData.userInfo = res;
            APP.getUserInfoCallback && typeof APP.getUserInfoCallback === "function" ? APP.getUserInfoCallback(res): null;
        }
        return res
    },

    /*
    * 修改电话号码
    * */
    editRegisterPhone: async function (data = {}) {
        let url = Config.api.editPhone;
        return await Ajax.request({
            method: "POST",
            url,
            data
        });
    },

    /*
    * 字典查询
    * */
    getDictList: async function (dictTypeCode) {
        let url = Config.api.dict;
        let res = await Ajax.request({
            method: "POST",
            url,
            data: {
                dictTypeCode
            }
        })
        return res.map(function (item) {
            return {text: item.name, code: item.code}
        })
    },

    /*
    * 文件上传
    * */
    uploader: async function (tempFilePaths) {
        let url = Config.api.upload;
        let token = getStorageSync(Config.storage.token);
        let header = {Authorization: token}
        return await uploadFile({
            url,
            header,
            filePath: tempFilePaths,
            name: "file"
        }) || "/"
    },

    /*
    * 文件上传
    * */
    downloadFile: async function (path) {
        let url = Config.api.downLoad;
        return await downloadFile({
            url: url + "?fileId=" + path
        })
    },

    /*
    * 文件删除
    * */
    deleteFile: async function (path) {
        let url = Config.api.delete;
        return await Ajax.request({
            url,
            data: {fileId: path}
        });
    },

    /*
    * 获取绑定邮箱
    * */
    getBindEmail: async function (createUser) {
        let url = Config.api.emailBind;
        let res = await Ajax.request({
            method: "POST",
            url,
            data: {createUser}
        })
        return res ? {
            id: res.id,
            email: res.email
        } : "";
    },

    /*
    * 编辑绑定邮箱
    * */
    editBindEmail: async function (data) {
        let url = Config.api.emailEdit;
        return await Ajax.request({
            method: "POST",
            url,
            data
        })
    },

    /*
    * 添加绑定邮箱
    * */
    addBindEmail: async function (email) {
        let url = Config.api.emailAdd;
        return await Ajax.request({
            method: "POST",
            url,
            data: {email}
        })
    },

    /*
    * 意见反馈
    *
    * */
    feedback: async function (data) {
        let url = Config.api.feedback;
        return await Ajax.request({
            method: "POST",
            url,
            data
        })
    },

    /*
    * 获取关于我们信息
    * */
    getAboutUs: async function () {
        let url = Config.api.aboutUs;
        return await Ajax.request({
            method: "POST",
            url
        })
    }

}

/*
* 登录完成
* */

module.exports = Common